<?php
namespace app\admin\controller;
use app\admin\model\Ad as AdModel;
use app\admin\controller\Common;
class Ad extends Common
{

    public function lst()
    {   $auth=new Auth();
        $ad=new AdModel();
        
        $ads=$ad->order('sort desc')->paginate(10);
        $this->assign('ads',$ads);
        return view();
	}


	    public function add(){
        if(request()->isPost()){
            $data=input('post.');
            $data['sort']=0;
            //print_r($data);
            $validate = \think\Loader::validate('Ad');
            if(!$validate->scene('add')->check($data)){
                $this->error($validate->getError());
            }
            $ad=new AdModel;
            if($ad->save($data)){
                $this->success('添加广告成功',url('lst'));
            }else{
                $this->error('添加广告失败！');
            }
        }
        $adcates=db("adcate")->select();
        $this->assign("adcates",$adcates);
        return view();
    }

    public function edit(){
        if(request()->isPost()){
            $data=input('post.');
            //print_r($data);
            $validate = \think\Loader::validate('Ad');
            if(!$validate->scene('edit')->check($data)){
                $this->error($validate->getError());
            }
            $ad=new AdModel;
            $save=$ad->update($data);
            if($save){
                $this->success('修改广告成功！',url('lst'));
            }else{
                $this->error('修改广告失败！');
            }
            return;
        }
        $ad=AdModel::find(input('id'));
        $this->assign('ad',$ad);
         $adcates=db("adcate")->select();
        $this->assign("adcates",$adcates);
        return view();
    }

    public function del(){
        $del=AdModel::destroy(input('id'));
        if($del){
           $this->success('删除广告成功！',url('lst')); 
        }else{
            $this->error('删除广告失败！');
        }
    }

    

}
